<?php

namespace app\api\model;

use app\api\service\Token;
use app\common\enums\StatusEnum;
use app\common\controller\Send;
use think\Db;

class Wxoauth extends \app\common\model\Wxoauth
{
    
    use send;
    /**
     * 用户登录授权
     * @param $phone
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function wxOauth($data)
    {
        
        $wxoauth = new Wxoauth();
        $unionid  = $data['unionid'];
        $user = self::getByUnionId($unionid);
        if($user){
            $uid   =  $user['id'];
            $share_key = $user['share_key'];
            $phone = $user['phone'];
            if($user['status'] == 0){
                return self::sendResult(108, [], '您的账号已被管理员冻结');
            }
        }else{
            $wxoauth->unionid = $data['unionid'];
            $wxoauth->nickname = $data['nickname'];
            $wxoauth->head_img = $data['head_img'];
            $wxoauth->share_key = self::shareKey();
            $wxoauth->status = StatusEnum::ENABLED;
            $wxoauth->save();
            $uid = $wxoauth->id;
            $share_key = $wxoauth->share_key;
            $phone = '';
        }
        
        $token = Token::generateToken();
        cache($token, $uid, 86400);
        Phone::login(true,$uid,$token);
        return self::sendResult(self::$okCode, [
            'uid' => $uid,
            'nickname' => $data['nickname'],
            'sex' => $user['sex'],
            'province' => $user['province'],
            'city' => $user['city'],
            'country' => $user['country'],
            'headImg' => $data['head_img'],
            'shareKey' => $share_key,
            'autoplay' => $user['autoplay']?1:0,
            'hasMobile' => $phone?1:0,
            'token' => $token,
        ]);
        
        
    }
    
    
    /**
     * 查询数据
     * @param $phone
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function selectUserInfo($phone,$password)
    {
        $oauth = new Phone();
        $user = self::getByPhoneId($phone);
        if($user){
            if($user['password']!=MD5($password)){
                return self::sendResult(141, [], '密码不正确');
            }else{
                return self::sendResult(self::$okCode, [
                    'uid' => $user['id'],
                    'nickname' => $user['nickname'],
                    'sex' => $user['sex'],
                    'province' => $user['province'],
                    'city' => $user['city'],
                    'country' => $user['country'],
                    'headImg' => $user['head_img'],
                    'shareKey' => $user['share_key'],
                    'autoplay' => $user['autoplay'],
                    'hasMobile' => 0,
                ]);
            }
        }else{
            $oauth->phone = $phone;
            $oauth->share_key = self::shareKey();
            $oauth->status = StatusEnum::ENABLED;
            $oauth->save();
            return self::sendResult(self::$okCode, [
                'uid' => $oauth->id,
                'nickname' => '',
                'sex' => '',
                'province' => '',
                'city' => '',
                'country' => '',
                'headImg' => '',
                'shareKey' => $oauth->share_key,
                'autoplay' => '',
                'hasMobile' => '',
            ]);
        }
    }
    
    
    /**
     * 验证码登录
     * @param $phone
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function codeCheckUser($phone)
    {
        $oauth = new Phone();
        $user = self::getByPhoneId($phone);
        if($user){
            return self::sendResult(self::$okCode, [
                    'uid' => $user['id'],
                    'nickname' => $user['nickname'],
                    'sex' => $user['sex'],
                    'province' => $user['province'],
                    'city' => $user['city'],
                    'country' => $user['country'],
                    'headImg' => $user['head_img'],
                    'shareKey' => $user['share_key'],
                    'autoplay' => $user['autoplay'],
                    'hasMobile' => 0,
                ]);
            
    
        }else{
            $oauth->phone = $phone;
            $oauth->share_key = self::shareKey();
            $oauth->status = StatusEnum::ENABLED;
            $oauth->save();
            return self::sendResult(self::$okCode, [
                'uid' => $oauth->id,
                'nickname' => '',
                'sex' => '',
                'province' => '',
                'city' => '',
                'country' => '',
                'headImg' => '',
                'shareKey' => $oauth->share_key,
                'autoplay' => '',
                'hasMobile' => '',
            ]);
        }
    }

    /**
     * 根据phone更新一条数据
     * @param $openid
     * @param $data
     * @return array
     * @throws \think\Exception\DbException
     */
    public static function updateOne($openid, $data)
    {
        $user = self::getByOpenId($openid);
        if (empty($user)) {
            $user = new Oauth;
            $user->openid = $openid;
            $user->share_key = self::shareKey();
            $user->status = StatusEnum::ENABLED;
            $user->autoplay = self::AUTOPLAY;
        }
        $user->unionid = isset($data['unionid']) ? $data['unionid'] : '';
        $user->nickname = $data['nickname'];
        $user->sex = $data['sex'];
        $user->province = $data['province'];
        $user->city = $data['city'];
        $user->country = $data['country'];
        $user->head_img = $data['head_img'];
        return $user->save() ? $user : [];
    }

    /**
     * 根据openid更新手机号码
     * @param $openid
     * @param $data
     * @return boolean
     * @throws \think\Exception\DbException
     */
    public static function updatePhone($openid, $data)
    {
        $user = self::getByOpenId($openid);
        if ($user) {
            $user->phone = $data['phone'];
            $user->pure_phone = $data['pure_phone'];
            return $user->save() ? true : false;
        }
        return false;
    }

    /**
     * 编辑个性签名
     * @param $oauthId
     * @param $content
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function signature($oauthId, $content)
    {
        $user = self::getOne($oauthId);
        if ($user) {
            $user->signature = $content;
            return $user->save() ? true : false;
        }
        return false;
    }

    /**
     * 编辑是否自动播放
     * @param $oauthId
     * @param $autoplay
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function autoplay($oauthId, $autoplay)
    {
        $user = self::getOne($oauthId);
        if ($user) {
            $user->autoplay = $autoplay;
            return $user->save() ? true : false;
        }
        return false;
    }

    /**
     * id随机key
     * @param int $length 长度
     * @return string
     * @throws \think\Exception\DbException
     */
    private static function shareKey($length = 10)
    {
        $key = rand_char($length);
        $next = true;
        while ($next) {
            $model = Oauth::where(['share_key' => $key])->find();
            if (empty($model)) {
                $next = false;
            } else {
                $key = rand_char($length);
            }
        }
        return $key;
    }
    
    public static function agreement()
    {
    
        $agreement = DB::table('oauth_agreement')->field('content')->order('id DESC')->find();
        if($agreement){
            return self::sendResult(self::$okCode, $agreement,'成功');
        }else{
            return self::sendResult(141, [],'没有数据');
        }
    
    
    }
}